<?php


namespace app\api\controller;


use app\admin\model\Paper;
use app\admin\model\PriceLog;
use app\api\model\Order;
use app\lib\MiniWechat;
use think\facade\Log;

class Pay
{
    public function wechatNotifyByPackagePay()
    {
        Log::record([
            'wechat_pay_res' => file_get_contents('php://input')
        ]);
        $wechat = new MiniWechat();
        try {
            $data = $wechat->getNOtify();
        } catch (\Exception $e) {
            return $e->getMessage();
        }

        $resultCode = $data['result_code'] ?? '';
        $returnCode = $data['return_code'] ?? '';
        if ($resultCode == 'SUCCESS' && $returnCode == 'SUCCESS') {
            $orderNum = $data['out_trade_no'];
            $order = Paper::where('wechat_order_num', $orderNum)->find();
            if (empty($order)) {
                return '订单不存在';
            }
            $order->is_pay = 1;
            $order->pay_time = date('Y-m-d H:i:s', strtotime($data['time_end']));
            $order->wechat_transaction_id = $data['transaction_id'];
            $order->pay_response_data = json_encode($data);
            $order->update_time = date('Y-m-d H:i:s');
            $order->save();
            echo $wechat->getNotifySuccessReply();
        }
    }
}